/**
 *  JSimProd - Java simulation code for production processes
 *  Copyleft Jorge Mira Yagüe <jorge.mira.yague@gmail.com>
 *  Universidad de Alicante 2009
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package gui.tools;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import lib.model.FactoryModel;
import lib.model.ICColor;
import lib.simul.Agenda;
import lib.simul.Event;
import lib.simul.EventType;
import lib.simul.Simulation;

/**
 *
 * @author jorge
 */
public class MyCanvas extends Canvas {

    private Simulation simulation;
    private Agenda agenda;
    private int width;
    private FactoryModel fm;
    private Font titleFont;
    private Font serviceFont;
    private Color txtColor;
    private ViewType type;
    private List<Bar> bars;
    private BufferStrategy strategy;

    public MyCanvas(Simulation simulation) {
        this.simulation = simulation;
        this.fm = simulation.getFactoryModel();
        this.txtColor = Color.white;
        this.titleFont = new Font("Arial", 0, 20);
        this.serviceFont = new Font("Arial", 1, 12);
        agenda = null;
    }

    public void Show(int n, int width, int height, ViewType type) {
        this.agenda = simulation.getResults().get(n);
        this.width = width;
        this.type = type;
        bars = new ArrayList<Bar>();
        setSize(width, height);

        createBufferStrategy(2);
        strategy = getBufferStrategy();
        this.repaint();


    }

    @Override
    public void paint(Graphics g1) {

        Graphics g = strategy.getDrawGraphics();
        if (agenda == null || g == null) {
            return;
        }
        int start = 0;
        int length = 0;
        int index;
        if (agenda.getEvents().size() <= 2) {
            return;
        }
        Event first = agenda.FirstEvent();
        Event last = agenda.LastEvent();
        double size = (double) (width - 50) / (last.getCalendar().getTime().getTime() -
                first.getCalendar().getTime().getTime());
        ArrayList<String> names = new ArrayList<String>();
        ArrayList<Integer> starts = new ArrayList<Integer>();
        String name = "";
        String text = "";
        ICColor color;

        //Draws the title of the graph
        g.setColor(txtColor);
        g.setFont(titleFont);
        if (type == ViewType.ITEMS) {
            g.drawString("Items view", 15, 50);
        } else if (type == ViewType.STATIONS) {
            g.drawString("Stations view", 15, 50);
        }

        //Draws the graph
        g.setFont(serviceFont);
        for (Event e : agenda.getEvents()) {
            if (e.getType() == EventType.START_SERVICE) {
                start = (int) (size * (e.getCalendar().getTime().getTime() -
                        first.getCalendar().getTime().getTime()));
                if (type == ViewType.ITEMS) {
                    name = "Item: " + e.getItem().getOrder() + ":" + e.getItem().getNumber();
                } else if (type == ViewType.STATIONS) {
                    name = "Station: " + e.getStationName();
                }
                index = names.indexOf(name);
                if (index == -1) {
                    index = names.size();
                    g.setColor(txtColor);
                    g.drawLine(25, (index + 2) * 50, width - 25, (index + 2) * 50);
                    g.setColor(txtColor);
                    g.drawString(name, 25, (index + 2) * 50 - 25);
                    names.add(name);
                    starts.add(start);
                } else {
                    starts.set(index, start);
                }
            } else if (e.getType() == EventType.END_SERVICE) {
                if (type == ViewType.ITEMS) {
                    text = "Station: " + e.getStationName();
                    name = "Item: " + e.getItem().getOrder() + ":" + e.getItem().getNumber();
                } else if (type == ViewType.STATIONS) {
                    text = "Item: " + e.getItem().getOrder() + ":" + e.getItem().getNumber();
                    name = "Station: " + e.getStationName();
                }
                index = names.indexOf(name);
                start = starts.get(index);
                length = (int) (size * (e.getCalendar().getTime().getTime() -
                        first.getCalendar().getTime().getTime())) - start;
                color = fm.getItemCategory(e.getItem().getCategory()).getColor();
                //Draws the rectangle
                int x = starts.get(index) + 25;
                int y = (index + 2) * 50 - 7;
                try {
                    Image left = ImageIO.read(getClass().getResource("/res/bars/" + color.toString() + "_L.png"));
                    Image center = ImageIO.read(getClass().getResource("/res/bars/" + color.toString() + "_C.png"));
                    Image right = ImageIO.read(getClass().getResource("/res/bars/" + color.toString() + "_R.png"));

                    if (length == 1) {
                        g.drawImage(center, x, y, this);
                    } else {
                        g.drawImage(left, x, y, this);
                        for (int i = 1; i < length; i++) {
                            g.drawImage(center, x + i - 1, y, this);
                        }
                        g.drawImage(right, x + length - 1, y, this);
                    }
                } catch (Exception ex) {
                }
                bars.add(new Bar(x, y, length, 14, text));
            }
        }

        strategy.show();
    }

    public String getText(int x, int y) {
        String text;
        for (Bar bar : bars) {
            text = bar.contains(x, y);
            if (text != null) {
                return text;
            }
        }
        return null;
    }
}
